package controller

import (
	"we-account-api/pkg/base"
	"we-account-api/pkg/request"
	"we-account-api/service"

	"github.com/gin-gonic/gin"
)

type UserController struct {
	base.Controller
}

var User = new(UserController)

func (u *UserController) Register(c *gin.Context) {
	var req request.RegisterRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		u.Error(c, err.Error())
		return
	}

	err := service.UserService.Register(req)
	if err != nil {
		u.Error(c, err.Error())
		return
	}

	u.Success(c, nil)
}

func (u *UserController) Login(c *gin.Context) {
	var req request.LoginRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		u.Error(c, err.Error())
		return
	}

	token, err := service.UserService.Login(req)
	if err != nil {
		u.Error(c, err.Error())
		return
	}

	u.Success(c, gin.H{"token": token})
}

func (u *UserController) GetUserInfo(c *gin.Context) {
	user, err := service.UserService.GetUserInfo(c.GetInt64("user_id"))
	if err != nil {
		u.Error(c, err.Error())
		return
	}
	u.Success(c, user)
}

func (u *UserController) GetUserList(c *gin.Context) {
	page, pageSize := u.GetPagination(c)

	users, total, err := service.UserService.GetUserList(page, pageSize)
	if err != nil {
		u.Error(c, err.Error())
		return
	}

	u.Success(c, gin.H{
		"list":  users,
		"total": total,
	})
}

// GetUserList
